package com.example.statistics.ui

import android.content.Intent
import android.os.Bundle
import com.example.statistics.R
import com.example.statistics.common.BaseActivity
import com.example.statistics.net.UrlConfig
import com.example.statistics.ui.bank.BankFlowListActivity
import com.example.statistics.ui.customer.CustomerListActivity
import com.example.statistics.ui.inventory.InventorySubListActivity
import com.example.statistics.ui.port.ExchangeRateListActivity
import com.example.statistics.ui.port.StorageListActivity
import com.example.statistics.ui.print.PrintActivity
import com.example.statistics.ui.product.ProductInfoListActivity
import com.example.statistics.ui.product.ProductTreeActivity
import com.example.statistics.ui.purchase.*
import com.example.statistics.ui.sales.MarketSalesDailyListActivity
import com.example.statistics.ui.sales.MarketSalesListActivity
import com.example.statistics.ui.sales.SalesStatisticsListActivity
import com.example.statistics.ui.yw.*
import com.example.statistics.utils.FileUtils
import com.example.statistics.utils.activityForResult
import com.jhj.httplibrary.HttpCall
import com.jhj.httplibrary.callback.DownloadHttpCallback
import com.jhj.slimadapter.SlimAdapter
import com.tencent.smtt.sdk.QbSdk
import kotlinx.android.synthetic.main.activity_recyclerview_refresh.*
import java.io.File
import java.io.FileInputStream
import java.io.FileWriter


class SplashActivity : BaseActivity() {

    override val title: String
        get() = "统计分析"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recyclerview_refresh)
        val map = mapOf(
            "主页" to MainActivity::class.java,
            "依汶原料" to MaterialListActivity::class.java,
            "依汶成品" to ProductSalesListActivity::class.java,
            "商品拆分" to PreProductProfitListActivity::class.java,
            "成品列表" to ProductTypeListActivity::class.java,
            "海关汇率" to ExchangeRateListActivity::class.java,
            "港口冷库" to StorageListActivity::class.java,
            "库存" to InventorySubListActivity::class.java,
            "蓝牙列表" to com.example.statistics.ui.shipment.ShipmentListActivity::class.java,
            "登录" to LoginActivity::class.java,
            "车" to com.example.statistics.ui.car.CarListActivity::class.java,
            "流向" to MarketSalesListActivity::class.java,
            "日报" to MarketSalesDailyListActivity::class.java,
            "产品信息" to ProductInfoListActivity::class.java,
            "产品树" to ProductTreeActivity::class.java,
            "采购" to PurchaseSpotProductActivity::class.java,
            "联系人" to CustomerListActivity::class.java,
            "代理" to PurchaseAgentListActivity::class.java,
            "融资" to TextActivity::class.java,
            "成本估算" to PurchaseAgentEstimateListActivity::class.java,
            "银行" to BankFlowListActivity::class.java,
            "销售统计" to SalesStatisticsListActivity::class.java,
            "蓝牙" to PrintActivity::class.java
        )

        SlimAdapter.creator()
            .register<String>(R.layout.list_item) { injector, bean, position ->
                injector.text(R.id.tv_part, bean)
                    .clicked {
                        val intent = Intent(this@SplashActivity, map[bean])
                        activityForResult(intent) {
                            val data = it?.getSerializableExtra("data")
                            val b = data
                        }

                    }
            }
            .attachTo(recyclerView)
            .setDataList(map.keys.toList())
        val url = UrlConfig.BASE_URL + "/upload/core/x5.tbs32.apk"
        val folder: String? = FileUtils.getSDPath("core")
        val path = "$folder/x5.tbs32.apk"
        //QbSdk.reset(this)
        if (!File(path).exists()) {
            HttpCall.download(url)
                .setUseBaseUrl(false)
                .enqueue(object : DownloadHttpCallback(this, File(path)) {
                    override fun onSuccess(file: File) {
                        QbSdk.installLocalTbsCore(this@SplashActivity, 46140, path)
                    }

                    override fun onFailure(msg: String) {
                    }
                })
        }

    }


    fun write(name: String, json: String) {
        val folder = FileUtils.getSDPath("file")
        val file = File("$folder/$name")
        val writer = FileWriter(file, true)
        writer.write(json)
        writer.close()
    }

    fun read(name: String): String {
        val stringBuffer = StringBuffer()
        val folder = FileUtils.getSDPath("file") + name
        val inStream = FileInputStream(folder);
        val buffer = ByteArray(1024)
        var byteread = inStream.read(buffer)
        //分行读取
        while (byteread != -1) {
            stringBuffer.append(String(buffer, 0, byteread))
            byteread = inStream.read(buffer)
        }
        inStream.close();
        return stringBuffer.toString();
    }
}